package com.sh.healthtv.view;

import android.content.Context;
import android.widget.RelativeLayout;
import android.widget.VideoView;


public class OperateVideoView {
	private MyVideoView mVideoView;
	private static OperateVideoView mInstance;
	// private Context mContext;
	private MyVideoView.VideoStateListener mListe;

	// private OperateVideoView(Context context,MyVideoView video) {
	private OperateVideoView() {
		super();
		// this.mContext = context;
	}

	// public static OperateVideoView getInstance(Context context,MyVideoView
	// video) {
	public static OperateVideoView getInstance() {
		// if (mInstance == null && video != null) {
		if (mInstance == null) {
			mInstance = new OperateVideoView();
		}
		return mInstance;
	}

	public void setListener(boolean isLive, MyVideoView.VideoStateListener lien) {
		this.mListe = lien;
		mVideoView.setListener(isLive, lien);
	}

	public void setURL(String path) {
		mVideoView.setVideoPath(path);
		start();
	}

	public void seekTo(int position) {
		mVideoView.seekTo(position);
		if (mListe != null) {
			mListe.OnSeekBarChange(position);
		}
	}

	public void pause() {
		mVideoView.pause();
	}

	public void start() {
		mVideoView.start();
	}

	public void terminate() {
		mVideoView.terminate();
	}

	public void reset() {
		mVideoView.cancelBgTimer();
	}
	// private boolean fullscreen = false;//全屏/窗口播放切换标志
	public void setZone(int marLeft, int marTop, Boolean type) {
		// // mVideoView.getParent();
		// RelativeLayout.LayoutParams layoutParams =
		// new
		// RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
		// RelativeLayout.LayoutParams.WRAP_CONTENT);
		// // layoutParams.setMargins(70, 120, 390, 120);
		// layoutParams.setMargins(marLeft, marTop, marRight, marBottom);
		// mVideoView.setLayoutParams(layoutParams);
		// mVideoView.invalidate();
		if (!type) {// 设置RelativeLayout的全屏模式
			RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.FILL_PARENT,
					RelativeLayout.LayoutParams.FILL_PARENT);
			layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
			layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
			layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
			layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
			mVideoView.setLayoutParams(layoutParams);

			// fullscreen = true;//改变全屏/窗口的标记
		} else {// 设置RelativeLayout的窗口模式

			RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
					marLeft, marTop);
			lp.addRule(RelativeLayout.CENTER_IN_PARENT);
			mVideoView.setLayoutParams(lp);
			// fullscreen = false;//改变全屏/窗口的标记
		}
	}

	public VideoView getVideo() {
		return mVideoView;
	}
	public void setVideo(MyVideoView video) {
		if (video == null) {
			return;
		}
		if (this.mVideoView != null) {
			mVideoView.terminate();
			mVideoView.stopPlayback();
			mVideoView = null;
		}
		this.mVideoView = video;
	}

	public void unListener() {
		if (this.mVideoView != null) {
			mVideoView.unListener();
		}
	}
	public void registeResever(Context context) {
		if (this.mVideoView != null) {
			mVideoView.registeResever(context);
		}
	}

	public void unRegisteResever(Context context) {
		if (this.mVideoView != null) {
			mVideoView.unRegisteResever(context);
		}
	}
}
